#!/bin/bash
#========================================================================
# Author: YSLin
# Email: 
# File Name: for.sh
# Description: 
#   All for loop example are collected here.

#   ctrl_break.sh - break的例子
#   ctrl_continue.sh - continue的例子

#   loop_bg.sh - 在背景進行重複處理
#   loop_over_cmd.sh - 對指令的輸出做重複處理
#   loop_over_files.sh - 對檔案的一覽表做重複處理
#   loop_pipe.sh - 將重複處理的輸出導到pipe中  
#   loop_redirect.sh - 將重複處理的輸出重新導向   

#   ansi_color.sh - 改變文字顏色

# Edit History: 
#   2011-03-28    File created.
#========================================================================
#number for loop
for i in 1 2 3 4 5
do
   echo "Welcome $i times"
done


#string for loop
for item in one "two !" 'three !' four\ !; do
	echo "$item"
done

#bash version 3.0+
for i in {1..5}
do
   #1 2 3 4 5
   echo "Welcome $i times"
done

#Bash v4.0+ 
echo "Bash version ${BASH_VERSION}..."
for i in {0..10..2}
do
	#0 2 4 6 8 10
	echo "Welcome $i times"
done

#The seq command print a sequence of numbers and it is here due to historical reasons
for i in $(seq 1 2 10) #man seq
do
   #1 3 5 7 9
   echo "Welcome $i times"
done

max=10
for i in $(seq 1 $max) #man seq
do
    echo $i
done

#C-like format for loop
for (( c=1; c<=5; c++ ))
do
	#1 2 3 4 5
	echo "Welcome $c times..."
done

#infinite for loop
function infiniteLoop(){
	for (( ; ; ))
	do
		echo "infinite loops [ hit CTRL+C to stop]"
	done
}

for file in /etc/*
do
	if [ "${file}" == "/etc/resolv.conf" ]
	then
		#grep -c 計算出現次數
		countNameservers=$(grep -c nameserver /etc/resolv.conf)
		echo "Total  ${countNameservers} nameservers defined in ${file}"
		break
	fi
done

function copy2bak(){
	FILES="$@"
	for f in $FILES
	do
		# if .bak backup file exists, read next file
		if [ -f ${f}.bak ]
		then
			echo "Skiping $f file..."
			continue  # read next file and skip cp command
		fi
		# we are hear means no backup file exists, just use cp command to copy file
		/bin/cp $f $f.bak
	done
}

#By default $IFS is set to the space character.
SAVEIFS=$IFS
#避免檔案名稱有空白的問題
IFS=$(echo -en "\n\b")
for f in *
do
  echo "$f"
done
# restore $IFS
IFS=$SAVEIFS

#========================================================================
#   ctrl_break.sh - break的例子
#========================================================================
for item in one two three STOP four five; do                                                                                                  
    [ "$item" = "STOP" ] && break  # 若是「STOP」的話，則中斷for迴圈                                                                          
    echo "$item"                                                                                                                              
done                 

#========================================================================
#   ctrl_continue.sh - continue的例子
#========================================================================
# 只顯示奇數
for n in 1 2 3 4 5 6 7 8 9 10; do
    r=`expr $n % 2`           # 計數a÷b的餘數
    [ $r -eq 0 ] && continue  # 若餘數為0，則進到下一次迴圈
    echo "$n is odd"          # 若為奇數的話，則顯示出來
done

#========================================================================
#   loop_bg.sh - 在背景進行重複處理
#========================================================================
for i in 5 4 3 2 1 "Fire!"; do
    echo "$i"
    sleep 1
done &

sleep 3

#========================================================================
#   loop_over_cmd.sh - 對指令的輸出做重複處理
#========================================================================
# 處理對象資料夾
dir="$1"

# 將區隔文字指定為換行字元
IFS='
'

# ls指令會在標準輸出不是終端的時候，將檔案逐行輸出。
# 由於IFS裡面只有設定換行文字，所以檔名中含有空白字元
# 也無所謂。
for name in `ls "$dir"`; do
    if [ -f "$dir/$name" ]; then
    echo "$dir/$name (regular file)"
    elif [ -d "$dir/$name" ]; then
    echo "$dir/$name (directory)"
    else
    echo "$dir/$name (other type)"
    fi  
done

#========================================================================
#   loop_over_files.sh - 對檔案的一覽表做重複處理
#========================================================================
for name in "$1"/*; do
    if [ -f "$name" ]; then
    echo "$name (regular file)"
    elif [ -d "$name" ]; then
    echo "$name (directory)"
    else
    echo "$name (other type)"
    fi  
done

#========================================================================
#   loop_pipe.sh - 將重複處理的輸出導到pipe中  
#========================================================================
for capital in London Paris Beijing Pusan Washington; do
    echo "$capital"
done | sort

#========================================================================
#   loop_redirect.sh - 將重複處理的輸出重新導向   
#========================================================================
for name in *.txt; do                                                                                                                         
    echo "$name"                                                                                                                              
done >tmp.out  

#========================================================================
#   ansi_color.sh - 改變文字顏色
#========================================================================
# 代入ESC字元                                                                                                                                 
ESC=`printf "\033"`                                                                                                                           
                                                                                                                                              
# echo_n - 不換行的echo指令                                                                                                                   
echo_n() {                                                                                                                                    
    echo $ECHO_N "$*"$ECHO_C                                                                                                                  
}                                                                                                                                             
                                                                                                                                              
# 設定echo指令的相容性                                                                                                                        
if [ -z `echo -n` ]; then                                                                                                                     
    # Solaris以外                                                                                                                             
    ECHO_N="-n"                                                                                                                               
    ECHO_C=""                                                                                                                                 
else                                                                                                                                          
    # Solaris的情況                                                                                                                           
    ECHO_N=""                                                                                                                                 
    ECHO_C="\c"                                                                                                                               
fi                                                                                                                                            
                                                                                                                                              
# 使文字顏色和屬性產生變化                                                                                                                    
for color in 30 31 32 33 34 35 36 37; do                                                                                                      
    for attr in 0 1 4 5 7; do                                                                                                                 
    echo_n "${ESC}[${attr};${color}mCOLOR${ESC}[0m "                                                                                          
    done                                                                                                                                      
    echo                                                                                                                                      
done                                                                                                                                          
                                                                                                                                              
# 使文字顏色和背景色產生變化                                                                                                                  
for fgcolor in 30 31 32 33 34 35 36 37; do                                                                                                    
    for bgcolor in 40 41 42 43 44 45 46 47; do                                                                                                
    echo_n "${ESC}[${fgcolor};${bgcolor}mCOLOR${ESC}[0m "                                                                                     
    done                                                                                                                                      
    echo                                                                                                                                      
done                                      
